/**
 * 特征配置相关API接口
 */
import { get, post, put, del } from '../utils/request'

// 模拟后端返回的特征配置数据
const mockFeatureConfigs = {
  // 默认特征配置
  default: [
    { key: 'clothingType', label: '服装类型', type: 'select', required: true },
    { key: 'color', label: '颜色', type: 'select', required: true },
    { key: 'pattern', label: '花色图案', type: 'select', required: false },
    { key: 'material', label: '材质特征', type: 'select', required: false },
    { key: 'sleeveType', label: '袖型', type: 'select', required: false },
    { key: 'collarType', label: '领型', type: 'select', required: false },
  ],

  // 高级特征配置（包含更多特征）
  advanced: [
    { key: 'clothingType', label: '服装类型', type: 'select', required: true },
    { key: 'color', label: '颜色', type: 'select', required: true },
    { key: 'pattern', label: '花色图案', type: 'select', required: false },
    { key: 'material', label: '材质特征', type: 'select', required: false },
    { key: 'sleeveType', label: '袖型', type: 'select', required: false },
    { key: 'collarType', label: '领型', type: 'select', required: false },
    { key: 'season', label: '适用季节', type: 'select', required: false },
    { key: 'style', label: '风格', type: 'select', required: false },
    { key: 'occasion', label: '适用场合', type: 'select', required: false },
    { key: 'ageGroup', label: '适用年龄', type: 'select', required: false },
  ],

  // 简化特征配置
  simple: [
    { key: 'clothingType', label: '服装类型', type: 'select', required: true },
    { key: 'color', label: '颜色', type: 'select', required: true },
    { key: 'material', label: '材质特征', type: 'select', required: false },
  ],
}

/**
 * 获取特征配置
 * @param {Object} params - 查询参数
 * @param {string} params.configType - 配置类型 (default/advanced/simple)
 * @param {string} params.batchId - 批次ID，用于获取特定批次的配置
 * @returns {Promise<Array>} 特征配置数组
 */
export const getFeatureConfig = async (params = {}) => {
  try {
    const response = await get('/feature-config', params)
    return response.data
  } catch (error) {
    console.error('获取特征配置失败:', error)
    // 如果API调用失败，返回默认配置作为fallback
    const { configType = 'default' } = params
    return mockFeatureConfigs[configType] || mockFeatureConfigs.default
  }
}

/**
 * 添加新的特征选项到字典
 * @param {Object} params - 参数
 * @param {string} params.category - 字典类别 (clothingType/color/pattern/material/sleeveType/collarType)
 * @param {string} params.name - 新选项名称
 * @param {string} params.batchId - 批次ID
 * @returns {Promise<Object>} 添加结果
 */
export const addDictionaryItem = async params => {
  try {
    const { category, name, batchId } = params
    // 使用新的路径格式：/{dict_type}/add
    const response = await post(`/dictionaries/${category}/add`, {
      name,
      batchId,
    })
    return response.data
  } catch (error) {
    console.error('添加字典条目失败:', error)
    throw error
  }
}

/**
 * 获取特征配置的选项数据
 * @param {Object} params - 查询参数
 * @param {Array} params.features - 特征配置数组
 * @param {string} params.batchId - 批次ID
 * @returns {Promise<Object>} 选项数据对象
 */
export const getFeatureOptions = async params => {
  try {
    const response = await get('/feature-options', params)
    return response.data
  } catch (error) {
    console.error('获取特征选项失败:', error)
    // 如果API调用失败，返回默认选项作为fallback
    const { features } = params
    const options = {}

    features.forEach(feature => {
      console.log(`处理特征 ${feature.key}:`, feature)
      switch (feature.key) {
        case 'clothingType':
          options[feature.key] = []
          break
        case 'color':
          options[feature.key] = []
          break
        case 'pattern':
          options[feature.key] = []
          break
        case 'material':
          options[feature.key] = []
          break
        case 'sleeveType':
          options[feature.key] = []
          break
        case 'collarType':
          options[feature.key] = []
          break
        case 'season':
          options[feature.key] = ['春季', '夏季', '秋季', '冬季', '四季']
          break
        case 'style':
          options[feature.key] = ['休闲', '正式', '运动', '时尚', '经典']
          break
        case 'occasion':
          options[feature.key] = ['日常', '工作', '聚会', '运动', '正式场合']
          break
        case 'ageGroup':
          options[feature.key] = ['婴儿', '幼儿', '儿童', '青少年', '成人']
          break
        default:
          options[feature.key] = []
      }
    })

    return options
  }
}
